<?php
    class Register extends CI_Controller{
        function __construct() {
            parent::__construct();
            $this->load->model('user','',true);
        }
        function index(){
            $this->load->view('reg_view');
        }
        function check_user_exist()
        {
            //GET DATA FORM REGISTER_VIEW BY POST METHOD
            $username = $this->input->post('username');
            $email = $this->input->post('email');
            $password = $this->input->post('password');
            $creation_date = date('Y-m-d',time());
            $language = $this->input->post('language');
            //GET IP ADDRESS FROM USER CLIENT
            if(!empty($_SERVER['HTTP_CLIENT_IP']))
            { 
                $ip = $_SERVER['HTTP_CLIENT_IP']; 
            }
            else if(!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
            { 
                $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; 
            }
            else
            { 
                $ip = $_SERVER['REMOTE_ADDR']; 
            } 
            //CALL CHECK EXISTED USER FUNCTION
            $result = $this->user->exist_user($username,$email);
            
            if($result)
            {
                //IF USERNAME OR E-MAIL DOES NOT EXISTED 
                //CALL ADD_USER FUNCTION TO INSERT NEW USER INTO DB
                $add_user_result = $this->user->add_user($username,$email,$password,$creation_date,$language,$ip);
                
                if($add_user_result)
                {
                    //ADD USER SUCESSFUL
                    echo "<script language='javascript'>alert('Registed Successful !')</script>";
                    $this->load->view('login_view');
                }
                else
                {
                    //ADD FAIL
                    $this->load->view('reg_view');
                }
            }
            else
            {
                //IF USERNAME OR E-MAIL EXISTED
                echo 'Username or e-mail existed!';
                echo "</br><a href='register'>Click here to back !</a>";
            }
        }
    }
?>
